import { UITransform } from "cc";
import { Layout, _decorator, Node } from "cc";
const { ccclass, property, requireComponent } = _decorator;

@ccclass('SimpleLayout')
export default class SimpleLayout extends Layout {

    @property({
        tooltip: "相对TopNode的边距",
    })
    autoSpace: boolean = false;

    private isInitAuto: boolean = false;

    protected onEnable(): void {
        super.onEnable();
        this.isInitAuto = false;
        this.type = Layout.Type.GRID;
        if (this.node.children.length > 0) {
            this.initAuto(this.node.children[0]);
        }
    }

    protected _childAdded(child: Node): void {
        super._childAdded(child);
        this.initAuto(child);
    }

    private initAuto(child: Node) {
        if (this.isInitAuto) return;
        if (!this.autoSpace) {
            console.log(`initAuto :: Error :: autoSpace = ${this.autoSpace}`);
            return;
        }
        if (this.type !== Layout.Type.GRID) {
            console.log(`initAuto :: Error :: type check = ${this.type} , autoSpace need to GRID`);
            return;
        }
        if (this.constraint === Layout.Constraint.NONE) {
            console.log(`initAuto :: Error :: constraint = NONE , autoSpace need to FIXED_ROW or FIXED_COL`);
            return;
        }
        if (this.constraintNum <= 0) {
            console.log(`initAuto :: Error :: constraintNum = ${this.constraintNum} constraintNum need > 0`);
            return;
        }


        if (this.constraint === Layout.Constraint.FIXED_COL) {
            // 固定列。设置 spaceX
            const transform_layout = this.node.getComponent(UITransform);
            const transform_child = child.getComponent(UITransform);
            if (transform_layout && transform_child) {
                const layout_width = transform_layout.width;
                const child_width = this.constraintNum * transform_child.width;
                if (layout_width > child_width) {
                    const space_width = layout_width - child_width;
                    const space = space_width / (this.constraintNum + 1);
                    if (space > 0) {
                        this.paddingLeft = space;
                        this.paddingRight = space;
                        this.paddingTop = space;
                        this.paddingBottom = space;
                        this.spacingX = space;
                        this.spacingY = space;
                        this.isInitAuto = true;
                    }
                }
            }
        } else {
            // 固定行。设置 spaceY
            const transform_layout = this.node.getComponent(UITransform);
            const transform_child = child.getComponent(UITransform);
            if (transform_layout && transform_child) {
                const layout_height = transform_layout.height;
                const child_height = this.constraintNum * transform_child.height;
                if (layout_height > child_height) {
                    const space_height = layout_height - child_height;
                    const space = space_height / (this.constraintNum + 1);
                    if (space > 0) {
                        this.paddingLeft = space;
                        this.paddingRight = space;
                        this.paddingTop = space;
                        this.paddingBottom = space;
                        this.spacingX = space;
                        this.spacingY = space;
                        this.isInitAuto = true;
                    }
                }
            }
        }

    }


}